<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2010
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/	
	
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);


// Basic parameter data
$mode		= request_var('mode', '');

if ($mode == 'transaction')
{	
	$user_id	= request_var('user_id', '');	

	// output HTTP header
	header("Content-type: application/csv");
	header("Content-Disposition: attachment; filename=TransactionReport-User$user_id.csv");
	header("Pragma: no-cache");
	header("Expires: 0");
	
	// output report info
	echo 'StockVirtual Transaction Report' . PHP_EOL;
	echo "User: $user_id , " . 'Check at,http://stockvirtual.info/query_user.php?mode=query_user&user_id=' . $user_id . PHP_EOL;
	echo 'Report generated at ' . date('Y/m/d H:i:s', get_taiwan_time()) . PHP_EOL;
	echo PHP_EOL;
	echo PHP_EOL;
	
	
	// output csv header
//	echo '代號 , 名稱, 張數, 類型, 委托時間, 價位, 成交金額, 交易稅, 成交時間, 處理時間, 處理狀態' . PHP_EOL;
	echo 'Corp ID, Share, Type, Request Time, Price, Total Amount, Tax, Deal Time, Check Time, Check Status' . PHP_EOL;
	
	
	// list all transactions
	$sql 	= 'SELECT * FROM sv_transaction WHERE user_id=' . $user_id . ' ORDER BY request_time DESC';		
	$result = $db->sql_query($sql); 
	while ($row = $db->sql_fetchrow($result))
	{
		if (is_valid_corp_id($row['corp_id']) == false)
		{
			continue;
		}
	
		$deal_time = 0;
		if ((int)$row['deal_time'] != 0)
		{
			$deal_time = get_time_string((int)$row['deal_time']);
		}
		$check_time = 0;
		if ((int)$row['check_time'] != 0)
		{
			$check_time = get_time_string((int)$row['check_time']);
		}			
		
		$str =  ' ' . $row['corp_id'] . ', ' . $row['share'] . ', ' . $row['action'] . ', ';
		$str .= ' ' . get_time_string((int)$row['request_time']) . ', ' . $row['price'] . ', ';
		$str .= ' ' . (round($row['price'] * $row['share'], 0)) . ', ' . (round($row['price'] * $row['share'] * SECURITY_TRANSACTION_TAX, 0)) . ', ';
		$str .= ' ' . $deal_time . ', ' . $check_time . ', ' . $row['check_status'] . PHP_EOL;
		
		echo $str;
				
		
	}		

}
else if ($mode == 'user_value')
{	
	$user_id	= request_var('user_id', '');	
	
	// output HTTP header
	header("Content-type: application/csv");
	header("Content-Disposition: attachment; filename=TransactionReport-User$user_id.csv");
	header("Pragma: no-cache");
	header("Expires: 0");
	
	// output report info
	echo 'StockVirtual User Value Report' . PHP_EOL;
	echo "User: $user_id , " . 'Check at,http://stockvirtual.info/query_user.php?mode=query_user&user_id=' . $user_id . PHP_EOL;
	echo 'Report generated at ' . date('Y/m/d H:i:s', get_taiwan_time()) . PHP_EOL;
	echo PHP_EOL;
	echo PHP_EOL;
	
	
	// output csv header
//	echo '代號 , 名稱, 張數, 類型, 委托時間, 價位, 成交金額, 交易稅, 成交時間, 處理時間, 處理狀態' . PHP_EOL;
	echo 'Date, value, money, TAIEX' . PHP_EOL;
	
	$sql 	= 'SELECT * FROM sv_user_value WHERE user_id=' . $user_id . ' ORDER BY date ASC';
	$result = $db->sql_query($sql); 
	while ($row = $db->sql_fetchrow($result))
	{
		$date	= $row['date'];
		$value	= (int)$row['value'];
		$money	= (int)$row['money'];
		
		$index = get_taiex($row['date']);
		if ($index != '')
			$taiex_index	= (int)($index);
		
		
		$str = ' ' . $date . ', ' . $value . ', ' . $money . ', ' . $taiex_index . PHP_EOL;
		echo $str;		
	}
}